<?php

namespace App\Http\Requests;

use Illuminate\Support\Facades\Validator;

abstract class PackingRequest
{

    public $realRequest;

    public function __construct()
    {
        $this->realRequest = request();
    }

    public function __call($name, $arguments)
    {
        return $this->realRequest->$name($arguments);
    }

    /**
     * @param array $other 额外验证
     * @param boolean $isUser 是否需要用户ID
     * @return array
     * @throws \App\Exceptions\ThrowException
     * @author yashuai<1762910894@qq.com>
     */
    public function getValidationData($other = [], $isUser = false)
    {
        $params = $this->realRequest->all();

        foreach ($params as $key => $val) {
            $val = is_string($val) ? trim($val) : $val;
            $params[$key] = !is_null($val) ? $val : '';
        }
        $rules    = !empty($other) && is_array($other) ? array_merge($other, $this->rules()) : $this->rules();
        $validate = Validator::make($params, $rules, $this->messages());
        $error    = $validate->errors()->first();
        if (!empty($error)) {
            _throw($error, 106002);
        }
        if ($isUser) {
            $key          = is_string($isUser) ? $isUser : 'UserID';
            $params[$key] = $this->realRequest->userId ?? 0;
        }
        return $params;
    }

    public abstract function rules();

    public abstract function messages();

}
